/*
Copyright (C) 2014  Johan Degraeve

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.

*/

package helpdiabetesj2me;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Johan
 */
public class ReportException implements CommandListener {  
     
     static String str_foutm[] = {"Foutmelding"};
     static String str_ok [] = {"Ok"};
    boolean exitapp;
    Command done ;

    Displayable curdisp;
    /**
    * Report an exception to the user, Alert will be shown with usertext, e.toString<br>
    *  
    *  After dismissing, and if not fatal, the previous displayable will be reshown
     * @param usertext to give more explanation
     * @param e the exception
     * @param fatal if true, application will be ended by this method, if false means application can continue
    */
    public void  Report(String usertext,Exception e, boolean fatal)  {
        exitapp=fatal;
        curdisp =  HelpDiabetes.midlet.getDisplay().getCurrent();
        
        Alert myalert = new Alert(str_foutm[HelpDiabetes.languageIndex],
                    usertext + 
                    "\n\ne.toString() = \n" + 
                    e.toString() +
                    "\n\ne.getMessage() =\n" + 
                    e.getMessage(),null,AlertType.ERROR);
        done = new Command(str_ok[HelpDiabetes.languageIndex],Command.EXIT,0);
        myalert.addCommand(done);
        myalert.setCommandListener(this);
        myalert.setTimeout(Alert.FOREVER);
        
        
        // switch display to myAlert
	HelpDiabetes.midlet.getDisplay().setCurrent(myalert);
                
        myalert = null;
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == done) {
            if (exitapp) {
                FoodFile.deletefoodfileRMS();
                try {HelpDiabetes.closeSelectedItemsRs();} catch (Exception ex) {;}
                try {RecordStore.deleteRecordStore("selecteditems");} catch (Exception ex) {;}        
                try {
                    HelpDiabetes.closeSelectedItemsRs();
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }
                HelpDiabetes.midlet.exitMIDlet();
            } else {
                HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
            }
        }
    }
   
}
